/**
 * Copyright 2009 Wisdom
 */
package com.wisdom.model.entities;

import java.util.Date;
import java.util.Set;

import javax.persistence.*;

/**
 * Entity represent of user in system.
 * 
 * @author alekseiko
 * @since 1.0
 */
@Entity
@Table(name = "WE_USERS")
public class User {

	private long id;
	private String login;
	private String password;
	private String email;
	private String name;
	private Date registrationDate;
	private Date enterDate;
	private boolean alive;
	private Set<Role> roles;
	private Set<Content> contents;
	private Set<Post> posts;
	private Set<Comment> comments;

	/**
	 * Default constructor
	 */
	public User() {
	}

	/**
	 * Constructor initialize all fields.
	 * 
	 * @param id
	 *            - unique user id in system
	 * 
	 * @param login
	 *            - user login
	 * @param password
	 *            - user password
	 * @param email
	 *            - user email
	 * @param name
	 *            - user full name
	 * @param registrationDate
	 *            - registration date
	 * @param enterDate
	 *            - last enter date
	 * @param alive
	 *            - if true then user is active in system, otherwise not active
	 * @param roles
	 *            - user roles in system
	 * @param contents
	 *            - user contents
	 * @param posts
	 *            - user posts
	 * @param comments
	 *            - user comments
	 */
	public User(long id, String login, String password, String email,
			String name, Date registrationDate, Date enterDate, boolean alive,
			Set<Role> roles, Set<Content> contents, Set<Post> posts,
			Set<Comment> comments) {
		this.id = id;
		this.login = login;
		this.password = password;
		this.email = email;
		this.name = name;
		this.registrationDate = registrationDate;
		this.enterDate = enterDate;
		this.alive = alive;
		this.roles = roles;
		this.contents = contents;
		this.posts = posts;
		this.comments = comments;
	}

	public boolean checkPassword(String password)
	{
		return this.password.equals(password);
	}
	
	/**
	 * Get unique user id in system
	 * 
	 * @return the id
	 */
	@Id
	@Column(name = "WE_ID")
	@GeneratedValue(strategy = GenerationType.SEQUENCE)
	public long getId() {
		return id;
	}

	/**
	 * Set unique user id in system
	 * 
	 * @param id
	 *            - unique user id
	 */
	public void setId(long id) {
		this.id = id;
	}

	/**
	 * Get user login
	 * 
	 * @return login
	 */
	@Column(name = "WE_LOGIN", unique = true, length = 45, nullable = false)
	public String getLogin() {
		return login;
	}

	/**
	 * Set user login.
	 * 
	 * @param login
	 */
	public void setLogin(String login) {
		this.login = login;
	}

	/**
	 * Get user password
	 * 
	 * @return the password
	 */
	@Column(name = "WE_PASSWORD", nullable = false, length = 255)
	public String getPassword() {
		return password;
	}

	/**
	 * Set user password.
	 * 
	 * @param password
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * Get user email
	 * 
	 * @return user email
	 */
	@Column(name = "WE_EMAIL", nullable = false, unique = true, length = 255)
	public String getEmail() {
		return email;
	}

	/**
	 * Set user email
	 * 
	 * @param email
	 *            - user email
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * Get user full name.
	 * 
	 * @return user full name
	 * 
	 */
	@Column(name = "WE_NAME", length = 255, updatable = true)
	public String getName() {
		return name;
	}

	/**
	 * Set user name.
	 * 
	 * @param name
	 *            - user full name.
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * Get registration date.
	 * 
	 * @return the registrationDate
	 */
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "WE_REG_DATE", nullable = false)
	public Date getRegistrationDate() {
		return registrationDate;
	}

	/**
	 * Set registration date
	 * 
	 * @param registrationDate
	 *            - date when user registered
	 */
	public void setRegistrationDate(Date registrationDate) {
		this.registrationDate = registrationDate;
	}

	/**
	 * Get last enter date.
	 * 
	 * @return the enterDate
	 */
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "WE_ENTER_DATE", nullable = false, updatable = true)
	public Date getEnterDate() {
		return enterDate;
	}

	/**
	 * Set last enter date.
	 * 
	 * @param enterDate
	 *            - last enter date
	 */
	public void setEnterDate(Date enterDate) {
		this.enterDate = enterDate;
	}

	/**
	 * Check alive flag.
	 * 
	 * @return the alive
	 */
	@Column(name = "WE_IS_ALIVE", nullable = false, updatable = true)
	public boolean isAlive() {
		return alive;
	}

	/**
	 * Set alive flag.
	 * 
	 * @param alive
	 *            - if true then user is active in system, otherwise not active
	 */
	public void setAlive(boolean alive) {
		this.alive = alive;
	}

	/**
	 * Get user roles
	 * 
	 * @return the roles
	 */
	@ManyToMany(targetEntity = com.wisdom.model.entities.Role.class, cascade = {
			CascadeType.PERSIST, CascadeType.MERGE })
	@JoinTable(name = "WE_USER_ROLES", joinColumns = @JoinColumn(name = "WE_USER_ID"), inverseJoinColumns = @JoinColumn(name = "WE_ROLES_ID"))
	public Set<Role> getRoles() {
		return roles;
	}

	/**
	 * Set user roles
	 * 
	 * @param roles
	 *            - user roles in system
	 */
	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}

	/**
	 * Get user contents
	 * 
	 * @return the contents
	 */
	@OneToMany(mappedBy = "owner")
	public Set<Content> getContents() {
		return contents;
	}

	/**
	 * Set user contents
	 * 
	 * @param contents
	 *            - user contents
	 */
	public void setContents(Set<Content> contents) {
		this.contents = contents;
	}

	/**
	 * Get user posts
	 * 
	 * @return the posts
	 */
	@OneToMany(mappedBy = "owner")
	public Set<Post> getPosts() {
		return posts;
	}

	/**
	 * Set user posts.
	 * 
	 * @param posts
	 *            - user posts
	 */
	public void setPosts(Set<Post> posts) {
		this.posts = posts;
	}

	/**
	 * Get user comments
	 * 
	 * @return the comments
	 */
	@OneToMany(mappedBy = "owner")
	public Set<Comment> getComments() {
		return comments;
	}

	/**
	 * Set user comments
	 * 
	 * @param comments
	 *            - user comments
	 */
	public void setComments(Set<Comment> comments) {
		this.comments = comments;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (int) (id ^ (id >>> 32));
		return result;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;

		User user = (User) obj;

		return user.getLogin().equals(getLogin())
				&& user.getPassword().equals(getPassword())
				&& user.getEmail().equals(getEmail());
	}

}
